package com.littlebee.storysuper.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.littlebee.storysuper.R;
import com.littlebee.storysuper.utils.Constant;
import com.littlebee.storysuper.utils.LogUtils;
import com.littlebee.storysuper.utils.ToastUtil;

import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.content.CustomContent;
import cn.jpush.im.android.api.content.EventNotificationContent;
import cn.jpush.im.android.api.content.ImageContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.content.VoiceContent;
import cn.jpush.im.android.api.event.ContactNotifyEvent;
import cn.jpush.im.android.api.event.MessageEvent;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.api.BasicCallback;

import static cn.jpush.im.android.api.event.ContactNotifyEvent.Type.invite_accepted;
import static cn.jpush.im.android.api.event.ContactNotifyEvent.Type.invite_received;

public class NotificationClickEvent extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_notification_click_event);
        JMessageClient.registerEventReceiver(this);

    }

    @Override
    protected void onDestroy() {
        JMessageClient.unRegisterEventReceiver(this);
        super.onDestroy();
    }
    public void onEvent(MessageEvent event){
        Message msg=event.getMessage();
        switch (msg.getContentType()){
            case text:
//                //处理文字消息
//                TextContent textContent = (TextContent) msg.getContent();
//                textContent.getText();
//                break;
            case image:
//                //处理图片消息
//                ImageContent imageContent = (ImageContent) msg.getContent();
//                imageContent.getLocalPath();//图片本地地址
//                imageContent.getLocalThumbnailPath();//图片对应缩略图的本地地址
//                break;
            case voice:
//                //处理语音消息
//                VoiceContent voiceContent = (VoiceContent) msg.getContent();
//                voiceContent.getLocalPath();//语音文件本地地址
//                voiceContent.getDuration();//语音文件时长
//                break;
            case custom:
//                //处理自定义消息
//                CustomContent customContent = (CustomContent) msg.getContent();
//                customContent.getNumberValue("custom_num"); //获取自定义的值
//                customContent.getBooleanValue("custom_boolean");
//                customContent.getStringValue("custom_string");
                Intent notificationIntent = new Intent(NotificationClickEvent.this, ChatActivity.class);
                startActivity(notificationIntent);//自定义跳转到指定页面
                break;
            case eventNotification:
                //处理事件提醒消息
                EventNotificationContent eventNotificationContent = (EventNotificationContent)msg.getContent();
                switch (eventNotificationContent.getEventNotificationType()){
                    case group_member_added:
                        //群成员加群事件
                        break;
                    case group_member_removed:
                        //群成员被踢事件
                        break;
                    case group_member_exit:
                        //群成员退群事件
                        break;
                    case group_info_updated://since 2.2.1
                        //群信息变更事件
                        break;
                }
                break;
        }

    }

    public void onEvent(ContactNotifyEvent event) {
        String reason = event.getReason();
        String fromUsername = event.getFromUsername();
        String appkey = event.getfromUserAppKey();

        switch (event.getType()) {
            case invite_received://收到好友邀请
                LogUtils.i("xhx","xhx:接收到好友请求："+event.getFromUsername());
                ContactManager.acceptInvitation(event.getFromUsername(), Constant.JPUSH_KEY, new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        if (0 == responseCode) {
                            //接收好友请求成功
                            ToastUtil.displayShortToast("接收好友请求成功");
                        } else {
                            //接收好友请求失败
                        }
                    }
                });                break;
            case invite_accepted://对方接收了你的好友邀请
                //...
                break;
            case invite_declined://对方拒绝了你的好友邀请
                //...
                break;
            case contact_deleted://对方将你从好友中删除
                //...
                break;
            default:
                break;
        }
    }
}
